<?php
	/**
	 * Elgg europass plugin
	 * 
	 * europass initialisation
	 *
	 */

		function europass_init()
    {
			// Load system configuration
			global $CONFIG;
			
			$config = find_plugin_settings('europass');
  		// todo : send message to user
  		if (!$config) return false;
  		
			// Load the language file
			register_translations($CONFIG->pluginspath . "europass/languages/");
				
			// Set up menu for logged in users
			if (isloggedin())
      {
				add_menu(elgg_echo('europass'), $CONFIG->wwwroot . "pg/europass/" . $_SESSION['user']->username);
				// And for logged out users
			}
      else
      {
				add_menu(elgg_echo('europass'), $CONFIG->wwwroot . "mod/europass/everyone.php",array());
			}
				
			// Extend system CSS with our own styles, which are defined in the europass/css view
			extend_view('css','europass/css');
			
			// Register a page handler, so we can have nice URLs
			register_page_handler('europass','europass_page_handler');
				
			// Register a URL handler for europass
			//register_entity_url_handler('europass_url','object','europass');
				
			// Register entity type
			register_entity_type('object','europass');
		}
		
		function europass_pagesetup()
    {
			global $CONFIG;
			//add submenu options
			if (get_context() == "europass")
      {
				if ((page_owner() == $_SESSION['guid'] || !page_owner()) && isloggedin())
        {
					add_submenu_item(elgg_echo('europass:your'),$CONFIG->wwwroot."pg/europass/" . $_SESSION['user']->username);
          $page_owner = page_owner_entity();
          if(get_user_objects($page_owner->getGUID(),'europass',1))
          {
						add_submenu_item(elgg_echo('europass:edit'),$CONFIG->wwwroot."mod/europass/edit.php");
					}
					add_submenu_item(elgg_echo('europass:friends'),$CONFIG->wwwroot."pg/europass/" . $_SESSION['user']->username . "/friends/");
					add_submenu_item(elgg_echo('europass:everyone'),$CONFIG->wwwroot."mod/europass/everyone.php");
				}
        else if (page_owner())
        {
          // Je suis par exemple sur l'europass d'un ami et je veux revenir au mien
					$page_owner = page_owner_entity();
					if ($page_owner instanceof ElggUser) // Sorry groups, this isn't for you.
						add_submenu_item(elgg_echo('europass:mine'),$CONFIG->wwwroot."pg/europass/" . $_SESSION['user']->username);
					add_submenu_item(elgg_echo('europass:everyone'),$CONFIG->wwwroot."mod/europass/everyone.php");
				}
        else
        {
					add_submenu_item(elgg_echo('europass:everyone'),$CONFIG->wwwroot."mod/europass/everyone.php");
				}
			}
		}
		
		/**
		 * europass page handler; allows the use of fancy URLs
		 *
		 * @param array $page From the page_handler function
		 * @return true|false Depending on success
		 */
		function europass_page_handler($page)
    {
			// The first component of a europass URL is the username
			if (isset($page[0]))
      {
				set_input('username',$page[0]);
			}
			// The second part dictates what we're doing
			if (isset($page[1]))
      {
				switch($page[1])
        {
					case "friends":		@include(dirname(__FILE__) . "/friends.php");
						return true;
						break;
				}
			// If the URL is just 'europass/username', or just 'europass/', load the standard europass index
			}
      else
      {
				@include(dirname(__FILE__) . "/index.php");
				return true;
			}
			return false;
		}

		
	// Make sure the pel initialisation function is called on initialisation
		register_elgg_event_handler('init','system','europass_init');
		register_elgg_event_handler('pagesetup','system','europass_pagesetup');
		
	// Register actions
		global $CONFIG;
		register_action("europass/add",false,$CONFIG->pluginspath . "europass/actions/add.php");
		register_action("europass/download",false,$CONFIG->pluginspath . "europass/actions/download.php");
		register_action("europass/delete",false,$CONFIG->pluginspath . "europass/actions/delete.php");
    register_action("europass/edit",false,$CONFIG->pluginspath . "europass/actions/edit.php");
?>